package image;


public class FakeDitherImagen extends BaseImagen {

	private static final long serialVersionUID = 8753791630365217203L;

	public FakeDitherImagen(String url) {
		super(url);
	}

	@Override
	protected int[][][] getRGBtoCMY() {
		int[][][] ret = new int[3][imagenOriginal.width][imagenOriginal.height];
		imagenOriginal.loadPixels();
		int[] rgb = new int[3];
			for (int i=0;i<this.tamX;i++)
				for (int j=0;j<this.tamY;j++) {
					int c = imagenOriginal.pixels[this.tamX*i+j];
					rgb = this.getRGBfromHex(c);
					
					ret[0][i][j] = 255-rgb[0];
					ret[1][i][j] = 255-rgb[1];
					ret[2][i][j] = 255-rgb[2];
				}
		return ret;
	}

	/** Fake dithering function that 
	 *  ONLY works for already dithered images!!!
	 *  don't use it for normal images.
	 */
	@Override
	protected int[][] getDither8() {
		this.imagenCMYK = this.getRGBtoCMY();
		
		for (int i=0;i<this.tamX;i++) 
			for (int j=0;j<this.tamY;j++) {
				int sum = 0;
				/*
				 * CONVERSION TABLE
				 * 
				 * C=1		M=3		Y=5
				 * 
				 * 		C	M	Y
				 * 0					= 0
				 * 1	X				= 1
				 * 2		X			= 3
				 * 3			X		= 5
				 * 4	X	X			= 4
				 * 5		X	X		= 8
				 * 6	X		X		= 6
				 * 7	X	X	X		= 9
				 * 
				 * 
				 * difungir
				 */
				if (this.imagenCMYK[0][i][j]!=0)
					sum+=1;
				if (this.imagenCMYK[1][i][j]!=0)
					sum+=3;
				if (this.imagenCMYK[2][i][j]!=0)
					sum+=5;
				
				switch(sum) {
				case 0: this.imagenDither[i][j]= 0; break;
				case 1: this.imagenDither[i][j]= 1; break;
				case 3: this.imagenDither[i][j]= 2; break;
				case 5: this.imagenDither[i][j]= 3; break;
				case 4: this.imagenDither[i][j]= 4; break;
				case 8: this.imagenDither[i][j]= 5; break;
				case 6: this.imagenDither[i][j]= 6; break;
				case 9: this.imagenDither[i][j]= 7; break;
				default: throw new RuntimeException("something strange happened");
				}
				
			}
		
		return this.imagenDither;
	}

}
